// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var leftMenu = require("../leftMenu/leftMenu");
    var topMenu = require("../topMenu/topMenu");
    var router = require("../hash/pageRouter");
    var css_detail_info = require("./logisticsCompany_details.css");
    var html_logisticsCompany_submit = require("./logisticsCompany_submit.html");
    var dialog = require("dialog");
    var validate = require("validate");
    var databind = require("databind");
    var tools = require("tools");
    var companyId = "";
    var init = function(page){
    	
		initEvent();
		companyId = page.params.companyId||base.getUrlParam("companyId");
		inquireLogisticsCompanyDetail(companyId);
		router.init();
    };
    
	var initEvent = function(){
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
		
		$(".button-submit").on("click",function(){
			 $("#popdialog").html(html_logisticsCompany_submit);
			 var logisticsCompany_dialog = $("#logisticsCompany_dialog");
    		dialog.showDialog(logisticsCompany_dialog, function() {});
    		databind.fillForm(".dialogContent",{companyId:companyId});
  			databind.fillInput($(".dialogContent"),{action:"inquireLogisticsCompanyDetail.json",version:"v3.0"});
  
    		updateLogisticsCompany(companyId,logisticsCompany_dialog);
    		$(".companyAccount").unbind("change").change(function(){
    				var companyAccount =	$(this).val();
					var myregex = new RegExp(" /^[0-9a-zA-Z]*$/g"); // 创建正则表达式 
					if (companyAccount.match(/[^A-Za-z0-9]/ig)){
							base.alert(2,"登录账户只允许字母和数字！");
							return ;
						} 
    				});
		});
    };
    
    var updateLogisticsCompany = function(companyId,logisticsCompany_dialog){
    	$("#sure").on("click",function(){
    	
    				var companyAccount =	$(".companyAccount").val();
					var myregex = new RegExp(" /^[0-9a-zA-Z]*$/g"); // 创建正则表达式 
					if (companyAccount.match(/[^A-Za-z0-9]/ig)){
							base.alert(2,"登录账户只允许字母和数字！");
							return ;
						} 
    		databind.submitDom($(".dialogContent"),function(){
    			inquireLogisticsCompanyDetail(companyId);
    			base.alert(1,"修改物流公司信息成功");
    			dialog.hideDialog(logisticsCompany_dialog);
    		});
    	});
    }
    
	var inquireLogisticsCompanyDetail = function(companyId){
		base.fnAjax({
            action: "inquireLogisticsCompanyDetail.json",
            oData: {
            	
            	companyId: companyId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	var logisticsCompany = data.datas[0];
                	$("#companyAccount_details").text(logisticsCompany.companyAccount);
                	$(".companyName_details").text(logisticsCompany.companyName);
                	$("#contactor_details").text(logisticsCompany.contactor);
                	$("#contactorTel_details").text(logisticsCompany.contactorTel);
                	var state = "";
                	if(logisticsCompany.reportState==0){
                		state = "没有";
                	}else{
                		state = "有";
                	}
                	$("#reportState_details").text(state);
                	if(logisticsCompany.effective==0){
                		state = "失效";
                	}else{
                		state = "有效";
                	}
                	$("#effective_details").text(state);
                	$("#remark_details").text(logisticsCompany.remark);
                } else {
                     base.alert(2,"信息获取失败");
                }
            }
        });
	}
	
    return {
        init:init
    };
});